package cn.caplike.demo.repository.gif;

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ImageUtils {

    public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
        Image image = sourceImage.getScaledInstance(width, height, BufferedImage.TYPE_INT_ARGB);
        BufferedImage zoomImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics gc = zoomImage.getGraphics();
        gc.setColor(Color.WHITE);
        gc.drawImage(image, 0, 0, null);
        return zoomImage;
    }

    public static void main(String[] args) throws Exception {
        GifDecoder decoder = new GifDecoder();
        int status = decoder.read("C:\\Users\\caplike\\Downloads\\测试gif类型.gif");
        if (status != GifDecoder.STATUS_OK) {
            throw new IOException("read gif error!");
        }

        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start("C:\\Users\\caplike\\Downloads\\测试gif类型.gifasdsa");
        encoder.setRepeat(decoder.getLoopCount());
        for (int i = 0; i < decoder.getFrameCount(); i++) {
            encoder.setDelay(decoder.getDelay(i));
            BufferedImage image = zoom(decoder.getFrame(i), 200, 200);
            encoder.addFrame(image);
        }
        encoder.finish();
    }
}